using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class LeaderboardController : MonoBehaviour
{
	public List<Text> names;

	public List<Text> scores;

	public Text mass;

	public Text killCount;

	public Text survivalTime;

	private WaitForSeconds wfs;

	private List<Character> characters;

	private PlayerCharacter playerCharacter;

	private void Awake()
	{
		wfs = new WaitForSeconds(0.5f);
		characters = new List<Character>();
		characters.AddRange(GameManager.Instance.characters);
		playerCharacter = GameManager.Instance.PlayerCharacter;
	}

	private void OnEnable()
	{
		StartCoroutine(UpdateLeaderboard());
	}

	private void OnDisable()
	{
		StopAllCoroutines();
	}

	private IEnumerator UpdateLeaderboard()
	{
		while (true)
		{
			characters.Sort((Character x, Character y) => y.Mass.CompareTo(x.Mass));
			for (int i = 0; i < characters.Count; i++)
			{
				characters[i].Rank = i + 1;
			}
			for (int j = 0; j < names.Count; j++)
			{
				names[j].text = characters[j].name;
				scores[j].text = characters[j].Mass.ToString();
				names[j].color = GameManager.Instance.colors[characters[j].Type];
			}
			if (playerCharacter.IsAlive)
			{
				mass.text = playerCharacter.Mass.ToString();
				killCount.text = playerCharacter.KillCount.ToString();
				survivalTime.text = playerCharacter.SurvivalTime.ToString("0");
			}
			yield return wfs;
		}
	}
}
